package com.irisking.ikaisdk;

import com.irisking.ikaisdk.dataStruct.DeviceConnInfo;
import com.irisking.ikaisdk.dataStruct.BasicJsonRec;
import com.irisking.ikaisdk.dataStruct.BasicJsonResp;
import com.irisking.ikaisdk.eventListener.DisConnectEventListener;
import com.irisking.ikaisdk.eventListener.EventListener;
import com.irisking.ikaisdk.handler.INetOpcode;
import com.irisking.server.callback.IDispatcher;
import com.irisking.server.coder.ConstantValue;
import com.irisking.ikaisdk.common.JacksonUtil;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Constructor;
import java.util.Date;

public class DispatchHandler implements IDispatcher {
    private AbstractServerSDK serverSDK ;
    public DispatchHandler(AbstractServerSDK serverSDK){
        this.serverSDK = serverSDK;
    }

    /**
     * 设备数据分发请求
     * @param commCode
     * @param key
     * @param data
     */
    @Override
    public void dispatcher(short commCode , String key , byte[] data) {
        String json = null;
        try {
            json = new String(data, 0, data.length, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        //System.out.println("req json:" + json);
        BasicJsonRec basicRec = JacksonUtil.getInstance().fromJson(json, BasicJsonRec.class);
        if (commCode == ConstantValue.REQ_TRANS_JSON_TO_SRV) {
            INetOpcode netCodeEntity = null;
            try {
                Class<?> clazz = Class.forName("com.irisking.ikaisdk.handler." + basicRec.getOperation());
                Constructor constructor = clazz.getConstructor(AbstractServerSDK.class); //构造函数参数列表的class类型
                netCodeEntity = (INetOpcode) constructor.newInstance(serverSDK);
                basicRec = netCodeEntity.deserialize(json);
                basicRec.setKey(key);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (netCodeEntity != null) {
                netCodeEntity.execute(basicRec);
            }
        } else if (commCode == ConstantValue.ACK_TRANS_JSON_TO_CLIENT) {
            BasicJsonResp resp = DeserializeRespOpcode(basicRec.getOperation(),json);
            // resp 数据返回
            serverSDK.addAckResp(resp);
            return;
        }
    }

    /**
     * 其他异常通知
     * @param e
     */
    @Override
    public void caughtException(Throwable e) {

    }

    /**
     * 设备离线通知
     * @param key
     */
    @Override
    public void offline(String key) {
        if (key!=null) {
            DeviceConnInfo deviceConnInfo = serverSDK.getClientByKey(key);
            serverSDK.removeClientByKey(key);
            EventListener handler = serverSDK.eventManager.getEventListener(DisConnectEventListener.class);
            if (handler != null) {
                // 执行回调通知上层
                ((DisConnectEventListener) handler).invoke(deviceConnInfo.getSn(), new Date());
            }
            System.out.println("删除客户端sn：" + deviceConnInfo.getSn());
        }
    }

    private BasicJsonResp DeserializeRespOpcode(String opCode, String json)
    {
        switch (opCode)
        {
            case "register":
                return null;
            case "deleteperson":
                return null;
            case "devicedetail":
                return null;
            case "getpersonlist":
                return null;
            case "getperson":
                return null;
            case "getdevicestate":
                return null;
            case "getlog":
                return null;
            case "getdeviceparam":
                return null;
            case "getpermission":
                return null;
            default:
                return null;
        }
    }

}
